package cloudspace.controlpanel.submitter;

import org.apache.log4j.Logger;
import org.webcat.submitter.SubmissionTargetException;
import org.webcat.submitter.targets.RootTarget;
import org.webcat.submitter.targets.SubmissionTarget;
import org.zkoss.zul.AbstractTreeModel;


public class SubmissionTargetTreeModel extends AbstractTreeModel
{
    /**
	 * 
	 */
    private static final long serialVersionUID = -4315246926186789342L;

    protected static Logger logger = Logger.getLogger( SubmissionTargetTreeModel.class );


    public SubmissionTargetTreeModel( RootTarget root )
    {
        super( root );
    }


    public Object getChild( Object arg0, int arg1 )
    {
        SubmissionTarget target = (SubmissionTarget)arg0;
        try
        {
            return target.getLogicalChildren()[arg1];
        }
        catch ( SubmissionTargetException e )
        {
            logger.error( "Submission Target Exception on submission", e );
        }
        return null;
    }


    public int getChildCount( Object arg0 )
    {
        SubmissionTarget target = (SubmissionTarget)arg0;
        try
        {
            int length = target.getLogicalChildren().length;
            logger.trace( "Found " + length + " submission targets" );
            return length;
        }
        catch ( SubmissionTargetException e )
        {
            e.printStackTrace();
        }
        return 0;
    }


    public boolean isLeaf( Object arg0 )
    {
        SubmissionTarget target = (SubmissionTarget)arg0;
        try
        {
            return target.getLogicalChildren().length == 0;
        }
        catch ( SubmissionTargetException e )
        {
            e.printStackTrace();
        }
        return true;
    }

}
